package com.bee.剑指offer;

/**
 * @version 1.0.0
 * @Author yong.Mr
 * @data 2021-03-13
 */
public class 二分法查找 {

    public static int binarySearch(int[] array, int value) {
        if(array.length <= 0) {
            return -1;
        }
        int low = 0;
        int high = array.length - 1;
        // 数组只有一个值的时候，所以需要<=
        while (low <= high) {
            int mid = (low + high)/2;
            if(array[mid] == value) {
                return mid;
            }
            if(value < array[mid]) {
                high = mid - 1;
            }
            if(value > array[mid]) {
                low = mid + 1;
            }
        }
        return -1;

    }

    public static void main(String[] args) {
        int[] array = new int[] {1,2,19,20,25,26,28};
        int value = binarySearch(array, 26);
        System.out.println(value);
    }
}
